package hlhj.fhp.supreme.activitys

import android.graphics.Bitmap
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.os.Environment
import com.cjt2325.cameralibrary.JCameraView
import com.cjt2325.cameralibrary.listener.JCameraListener
import fhp.hlhj.giantfold.utils.LogUtil
import hlhj.fhp.supreme.BaseAty
import hlhj.fhp.supreme.R
import hlhj.fhp.supreme.javabean.TempBean
import kotlinx.android.synthetic.main.activity_camera_aty.*
import org.greenrobot.eventbus.EventBus
import java.io.File
import javax.xml.transform.ErrorListener

class CameraAty : BaseAty() {
    private var type:Int=0;
    override fun getContentId(): Int {
        return R.layout.activity_camera_aty
    }
    private lateinit var bitmap:Bitmap
    private var path=""
    private val CAMERA_VIDEO: Int=110

    private val CAMERA_IMG: Int=120

    override fun initView() {
        camera.setSaveVideoPath(Environment.getExternalStorageDirectory().getPath() + File.separator + "JCamera");
        when(intent.getIntExtra("type",-1)){
            0->{
                camera.setFeatures(JCameraView.BUTTON_STATE_ONLY_CAPTURE);
            }
            1->{
                camera.setFeatures(JCameraView.BUTTON_STATE_ONLY_RECORDER);
            }
        }
//设置视频质量
        camera.setMediaQuality(JCameraView.MEDIA_QUALITY_MIDDLE);
        camera.setErrorLisenter(object : com.cjt2325.cameralibrary.listener.ErrorListener {
            override fun onError() {
                LogUtil.e("info","录制失败")
//                TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
            }

            override fun AudioPermissionError() {
                LogUtil.e("info","录音权限")
//                TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
            }
        })
        camera.setJCameraLisenter(object : JCameraListener {
            override fun recordSuccess(url: String?, firstFrame: Bitmap?) {
                LogUtil.e("info","录制完成")

                type=CAMERA_VIDEO
                bitmap=firstFrame!!
                path=url!!

                val bean = TempBean(firstFrame, url, CAMERA_VIDEO)
                EventBus.getDefault().post(bean)
                finish()
//                handler.sendEmptyMessage(1)
                LogUtil.e("info","录制完成6")

            }

            override fun captureSuccess(bit: Bitmap?) {
                type=CAMERA_IMG
                bitmap=bit!!
                val bean = TempBean(bitmap, "", CAMERA_IMG)
                EventBus.getDefault().post(bean)
                finish()

            }
        })
        camera.setLeftClickListener {
            finish()
        }

    }
}
